module SZPB(clk50m,rst,lcd_en,lcd_rw,lcd_rs,lcd_data,lcd_on);

	input clk50m;
	input rst;
	output lcd_en;
	output lcd_rw;
	output lcd_rs;
	output lcd_on;
	//output lcd_bg;
	output reg[7:0] lcd_data;reg lcd_rw,lcd_rs, clk4hz, lcd_on;

	reg[10:0] state;
	reg[6:0] count;
	reg[5:0] disp_count;
	reg[23:0] cnt;
	reg[255:0] data_in_buf ;

	parameter CLEAR=8'b00000001 ;
	parameter SETCGRAM =8'b00000010;
	parameter SETFUNCTION=8'b00000100;
	parameter SWITCHMODE=8'b00001000 ;
	parameter SETMODE=8'b00100000 ;

	parameter RETURNCURSOR =8'b00100000;
	parameter SHIFT=81'b01000000 ;
	parameter WRITERAM=8'b10000000;

	parameter CUR_INC=1 ;
	parameter CUR_NOSHIFT=0;
	parameter OPEN_DISPLAY=1;
	parameter OPEN_CUR=0;
	parameter BLANK_CUR=0 ;
	parameter DATAWIDTH8=1;
	parameter DATAWIDTH4=0;
	parameter TWOLINE=1;
	parameter ONELINE=0;
	parameter FONT5X10=1;
	parameter FONT5X7=0;

	parameter DATA_IN= "   One World       One Dream    ";

	//assign lcd_bg=1 ;
	always@(posedge clk50m)
		begin if (cnt==23'h5F5E10)
					begin cnt<=0; clk4hz<=~clk4hz;
					end
				else cnt<=cnt+1 ;
		end
	assign lcd_en=clk4hz ;

	always@(posedge clk4hz or posedge rst)
		if (rst)
			begin
				state <= CLEAR;
				lcd_on <=0 ; disp_count <=6'b0;
			end
		else begin
			lcd_on <=1 ;
			case(state)

				CLEAR: begin  lcd_rs<=0;lcd_rw<=0;
					lcd_data<=8'b00000001;
					data_in_buf<=DATA_IN;

					state<=SETCGRAM ;end

				SETCGRAM:begin
					lcd_rs<=0;lcd_rw<=0 ;
					lcd_data <= 8'b10000000 ; state <= SETFUNCTION; end

				SETFUNCTION : begin
					lcd_rs<=0;lcd_rw<=0;
					lcd_data[7:5]<=3 'b001 ;lcd_data [4] <=DATAWIDTH8 ;lcd_data [3] <=TWOLINE ;
					lcd_data [2] <= FONT5X10 ;
					lcd_data[1:0] <=2 'b00 ;
					state <= SWITCHMODE;end

				SWITCHMODE: begin
					lcd_rs<=0;lcd_rw<=0;
					lcd_data[7:3]<=5 'b00001 ;lcd_data [2] <=OPEN_DISPLAY;lcd_data [1] <=OPEN_CUR;lcd_data [0] <=BLANK_CUR;
					state <= SETMODE;end

				SETMODE:begin
					lcd_rs<=0;lcd_rw<=0;
					lcd_data[7:2]<=6'b000001 ;lcd_data [1] <=CUR_INC;lcd_data [0] <=CUR_NOSHIFT;
					state <= WRITERAM;end

				RETURNCURSOR:begin
					lcd_rs<=0;lcd_rw<=0;
					lcd_data<=8'b00000010 ;
					state <= WRITERAM;end

				SHIFT:begin
					lcd_rs<=1;lcd_rw<=0;
					lcd_data<=data_in_buf[255:248];
					data_in_buf <=(data_in_buf<<8);
					disp_count<=disp_count+1'b1;
					state <= WRITERAM;end

				WRITERAM: begin
					lcd_rs<=1;lcd_rw<=0;
					if(disp_count==32)
						begin
							disp_count<=4'b0;state<=CLEAR;
						end
					else if(disp_count==16)
						begin
							lcd_rs<=0;lcd_rw<=0;
							lcd_data<=8'b11000000;
							state<=SHIFT;
						end
					else begin
						lcd_data<=data_in_buf [255:248] ;
						data_in_buf<= (data_in_buf<<8) ;
						disp_count<=disp_count+1 'b1 ;
						state<=WRITERAM;end

				end	
		endcase
	end

endmodule